package com.yycx.module.bus.provider.handler.haiying.listener;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderSendStateListener implements JavaDelegate {
    @Autowired
    private RuntimeService runtimeService;

    @Override
    public void execute(DelegateExecution execution) {
        boolean conditionMet = false;
        int attemptCount = 0;
        execution.setVariable("sendState", 0);
        while (!conditionMet && attemptCount < 100) {

            // 检查是否满足条件
            conditionMet = checkCondition(execution);

            if (!conditionMet) {

                // 如果条件不满足，等待一段时间后重新检查
                try {
                    Thread.sleep(5000); // 等待 5 秒
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                attemptCount++;
            }
        }

        if (conditionMet) {
            // 条件满足，继续流程
            execution.setVariable("conditionMet", true);
        } else {
            // 条件仍未满足，可以选择终止流程或采取其他操作
            //throw new RuntimeException("Condition not met after multiple attempts.");
        }
    }

    private boolean checkCondition(DelegateExecution execution) {
        // 实现你的条件检查逻辑
        // 返回 true 如果条件满足，否则返回 false
        return false;
    }



/*    <bpmn:boundaryEvent id="timerEvent" attachedToRef="myTask">
    <bpmn:timerEventDefinition>
        <bpmn:timeDuration>PT5S</bpmn:timeDuration> <!-- 每 5 秒触发一次 -->
    </bpmn:timerEventDefinition>
    <bpmn:extensionElements>
        <flowable:executionListener class="com.example.OrderSendStateListener" event="start"/>
    </bpmn:extensionElements>
</bpmn:boundaryEvent>*/
}